﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AG.Editor.Core.Data;
using AG.Editor.Windows;
using AG.Editor.Core;

namespace AG.Editor.TerrainUI.Windows
{
    public partial class AGCreateTerrainWindow : Form
    {
        public AGTerrain Terrain { get; private set; }

        public AGCreateTerrainWindow()
        {
            InitializeComponent();

            _ctlCheckSimple.Checked = true;
            _ctlListForeground.Enabled = false;
            _ctlListBackground.Enabled = false;

            _ctlListForeground.DataSource = AGEContext.Current.EProject.Terrains;
            _ctlListBackground.DataSource = AGEContext.Current.EProject.Terrains;
        }

        private void _ctlBtnCreate_Click(object sender, EventArgs e)
        {
            if (_ctlBtnSelectModel.Tag == null)
            {
                return;
            }

            Terrain = new AGTerrain();
            Terrain.Id = Convert.ToByte(_ctlEditId.Text);

            // 检查地形编号是否存在
            for (int index = 0; index < AGEContext.Current.EProject.Terrains.Count; index++)
            {
                if (AGEContext.Current.EProject.Terrains[index].Id == Terrain.Id)
                {
                    MessageBox.Show(string.Format("地形编号{0}已存在:{1}", Terrain.Id, AGEContext.Current.EProject.Terrains[index]));
                    return;
                }
            }

            Terrain.Name = _ctlEditName.Text;
            if (_ctlCheckSimple.Checked)
            {
                Terrain.ForegroundId = Terrain.Id;
                Terrain.BackgroundId = Terrain.Id;
            }
            else
            {
                Terrain.ForegroundId = (_ctlListForeground.SelectedItem as AGTerrain).Id;
                Terrain.BackgroundId = (_ctlListBackground.SelectedItem as AGTerrain).Id;
            }

            AGModel selModel = _ctlBtnSelectModel.Tag as AGModel;
            Terrain.ModelUniqueId = selModel.UniqueId;
            for (int index = 0; index < selModel.Actions.Count; index++)
            {
                AGAction modelAction = selModel.Actions[index];

                AGTerrainTile tile = new AGTerrainTile();
                tile.Id = (byte)modelAction.Id;
                tile.Caption = modelAction.Caption;
                tile.Count = (byte)modelAction.GetDirection(0).Frames.Count;
                Terrain.Tiles.Add(tile);
            }

            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void _ctlBtnSelectModel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            AGSelectModelWindow window = new AGSelectModelWindow(AGEContext.Current.EProject);
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                _ctlBtnSelectModel.Text = window.SelectedModel.Caption;
                AGModel model = AGTools.Current.ModelStore.GetModel(AGEContext.Current.EProject, window.SelectedModel);
                _ctlBtnSelectModel.Tag = model;
            }
        }

        private void _ctlCheckSimple_CheckedChanged(object sender, EventArgs e)
        {
            if (_ctlCheckSimple.Checked)
            {
                _ctlListForeground.Enabled = false;
                _ctlListBackground.Enabled = false;
            }
            else
            {
                _ctlListForeground.Enabled = true;
                _ctlListBackground.Enabled = true;
            }
        }
    }
}
